<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content>
    <meta name="keyword" content>
    <link rel="shortcut icon" href="/img/ironman-draw.png">
    <!-- Place this tag in your head or just before your close body tag. -->
    <script async defer src="https://buttons.github.io/buttons.js"></script>
    <title>
        
          go设计令牌锁实现文件并发顺序读取 - Congz Blog
        
    </title>

    <link rel="canonical" href="http://congz.top/2021/03/01/go令牌锁/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS --> 
    <link rel="stylesheet" href="/css/beantech.min.css">
    
    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <link rel="stylesheet" href="/css/widget.css">

    <link rel="stylesheet" href="/css/rocket.css">

    <link rel="stylesheet" href="/css/signature.css">

    <link rel="stylesheet" href="/css/toc.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">
	<!-- Modified by Yu-Hsuan Yen -->
<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        
            background-image: url('bkg.jpg')
            /*post*/
        
    }
    
    #signature{
        background-image: url('/null');
    }
    
</style>

<header class="intro-header" >
    <!-- Signature -->
    <div id="signature">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                
                    <div class="post-heading">
                        <div class="tags">
                            
                              <a class="tag" href="/tags/#golang" title="golang">golang</a>
                            
                        </div>
                        <h1>go设计令牌锁实现文件并发顺序读取</h1>
                        <h2 class="subheading"></h2>
                        <span class="meta">
                            Posted by Congz on
                            2021-03-01
                        </span>
                    </div>
                


                </div>
            </div>
        </div>
    </div>
</header>

	
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Congz</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/about/">About</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/archive/">Archives</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/tags/">Tags</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    <!-- Modify by Yu-Hsuan Yen -->

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <h1><span id="golang-设计令牌锁并发顺序读取文件">golang 设计令牌锁并发顺序读取文件</span></h1>
<p>在做计算机网络实验构建模拟网桥中，有一步操作是从两个文件交替读取信息，单纯使用 for 循环不能保证其速度，单纯使用并发不能保证顺序，所以想到使用令牌锁的方法。</p>
<h2><span id="实现思路">实现思路</span></h2>
<p>首先定义令牌，为空结构体</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义令牌 用于实现令牌锁</span></span><br><span class="line"><span class="keyword">type</span> Token <span class="keyword">struct</span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<p>若有文件读取完毕时，令牌就只放到自己的桶里，所以定义一个信号量来记录</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 管道关闭的信号量</span></span><br><span class="line"><span class="keyword">var</span> closed <span class="keyword">int</span></span><br></pre></td></tr></table></figure>
<p>因为有两个文件，所以定义两个桶来装令牌，令牌桶使用 channel 设计</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// channel切片用于进行任务编排，实现并发轮流读取文件</span></span><br><span class="line">chs := []<span class="keyword">chan</span> Token&#123;<span class="built_in">make</span>(<span class="keyword">chan</span> Token, <span class="number">1</span>), <span class="built_in">make</span>(<span class="keyword">chan</span> Token, <span class="number">1</span>)&#125;</span><br></pre></td></tr></table></figure>
<p>首先把令牌放入第一个 goroutine 的桶</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chs[<span class="number">0</span>] &lt;- <span class="keyword">struct</span>&#123;&#125;&#123;&#125;</span><br></pre></td></tr></table></figure>
<p>以上是令牌锁准备工作，下面是 goroutine 执行操作</p>
<p>第一步是查询自己的桶内是否有令牌，若有，继续执行，否则阻塞</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取令牌，否则阻塞</span></span><br><span class="line">token := &lt;-chs[portInt<span class="number">-1</span>]</span><br></pre></td></tr></table></figure>
<p>执行完网桥存储转发操作后，判断另一个文件是否已经被读完，如没有读完，释放令牌到另一个 goroutine 的桶里，否则释放令牌到自己的桶里</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 释放令牌，并把令牌给另外一个文件</span></span><br><span class="line"><span class="comment">// close判断另外一个文件是否读取完了</span></span><br><span class="line"><span class="comment">// 如果读取完了就把令牌交给自己的通道</span></span><br><span class="line"><span class="keyword">if</span> closed == <span class="number">0</span> &#123;</span><br><span class="line">	chs[portInt%<span class="number">2</span>] &lt;- token</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	chs[portInt<span class="number">-1</span>] &lt;- token</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2><span id="完整代码">完整代码</span></h2>
<p>以下是完整代码</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"io/ioutil"</span></span><br><span class="line">	<span class="string">"strconv"</span></span><br><span class="line">	<span class="string">"strings"</span></span><br><span class="line">	<span class="string">"sync"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// go run main.go</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义令牌 用于实现令牌锁</span></span><br><span class="line"><span class="keyword">type</span> Token <span class="keyword">struct</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于等待goroutine执行完毕</span></span><br><span class="line"><span class="keyword">var</span> wg sync.WaitGroup</span><br><span class="line"></span><br><span class="line"><span class="comment">// 管道关闭的信号量</span></span><br><span class="line"><span class="keyword">var</span> closed <span class="keyword">int</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="comment">// 转发表</span></span><br><span class="line">	forwardTable := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">string</span>)</span><br><span class="line">	<span class="comment">// channel切片用于进行任务编排，实现并发轮流读取文件</span></span><br><span class="line">	chs := []<span class="keyword">chan</span> Token&#123;<span class="built_in">make</span>(<span class="keyword">chan</span> Token, <span class="number">1</span>), <span class="built_in">make</span>(<span class="keyword">chan</span> Token, <span class="number">1</span>)&#125;</span><br><span class="line">	wg.Add(<span class="number">2</span>)</span><br><span class="line">	<span class="comment">// 并发执行读文件操作</span></span><br><span class="line">	<span class="comment">// 文件s1.txt视为端口一</span></span><br><span class="line">	<span class="keyword">go</span> readFile(<span class="string">"s1.txt"</span>, chs, forwardTable, <span class="string">"1"</span>)</span><br><span class="line">	<span class="comment">// 文件s2.txt视为端口二</span></span><br><span class="line">	<span class="keyword">go</span> readFile(<span class="string">"s2.txt"</span>, chs, forwardTable, <span class="string">"2"</span>)</span><br><span class="line">	<span class="comment">//首先把令牌交给第一个文件</span></span><br><span class="line">	chs[<span class="number">0</span>] &lt;- <span class="keyword">struct</span>&#123;&#125;&#123;&#125;</span><br><span class="line">	wg.Wait()</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 打印转发表</span></span><br><span class="line">	fmt.Println()</span><br><span class="line">	fmt.Println(<span class="string">"转发表："</span>)</span><br><span class="line">	fmt.Println(<span class="string">"目的地址\t端口号"</span>)</span><br><span class="line">	<span class="keyword">for</span> key, value := <span class="keyword">range</span> forwardTable &#123;</span><br><span class="line">		fmt.Printf(<span class="string">"%s\t\t%s\n"</span>, key, value)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">readFile</span><span class="params">(filePath <span class="keyword">string</span>, chs []<span class="keyword">chan</span> Token, table <span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">string</span>, port <span class="keyword">string</span>)</span></span> &#123;</span><br><span class="line">	<span class="keyword">defer</span> wg.Done()</span><br><span class="line">	portInt, err := strconv.Atoi(port)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		fmt.Println(<span class="string">"字符串转换失败"</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//读取文件</span></span><br><span class="line">	fileBytes, err := ioutil.ReadFile(filePath)</span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		fmt.Println(<span class="string">"读取文件失败"</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	lines := strings.Split(<span class="keyword">string</span>(fileBytes), <span class="string">"\n"</span>)</span><br><span class="line">	<span class="keyword">for</span> i, v := <span class="keyword">range</span> lines &#123;</span><br><span class="line">		<span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">			<span class="keyword">continue</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 获取令牌，否则阻塞</span></span><br><span class="line">		token := &lt;-chs[portInt<span class="number">-1</span>]</span><br><span class="line">		data := strings.Split(v, <span class="string">"	"</span>)</span><br><span class="line">		forwardAndStore(table, data, port)</span><br><span class="line">		<span class="comment">// 释放令牌，并把令牌给另外一个文件</span></span><br><span class="line">		<span class="comment">// close判断另外一个文件是否读取完了</span></span><br><span class="line">		<span class="comment">// 如果读取完了就把令牌交给自己的通道</span></span><br><span class="line">		<span class="keyword">if</span> closed == <span class="number">0</span> &#123;</span><br><span class="line">			chs[portInt%<span class="number">2</span>] &lt;- token</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			chs[portInt<span class="number">-1</span>] &lt;- token</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 读取完文件就关闭通道</span></span><br><span class="line">	closed = <span class="number">1</span></span><br><span class="line">	<span class="built_in">close</span>(chs[portInt<span class="number">-1</span>])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">forwardAndStore</span><span class="params">(table <span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">string</span>, data []<span class="keyword">string</span>, port <span class="keyword">string</span>)</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> s [<span class="number">3</span>]<span class="keyword">string</span></span><br><span class="line">	<span class="keyword">for</span> n, v := <span class="keyword">range</span> data &#123;</span><br><span class="line">		s[n] = v</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> s[<span class="number">0</span>] == <span class="string">""</span> &#123;</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Printf(<span class="string">"从端口%s接收到数据帧%s\n"</span>, port, s[<span class="number">2</span>])</span><br><span class="line">	<span class="comment">// 自学习，查找转发表中源地址的项目</span></span><br><span class="line">	<span class="keyword">if</span> table[s[<span class="number">0</span>]] != <span class="string">""</span> &#123;</span><br><span class="line">		table[s[<span class="number">0</span>]] = port</span><br><span class="line">		fmt.Printf(<span class="string">"已存在该条目，更新后源地址%s,端口号%s\n"</span>, s[<span class="number">0</span>], port)</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		table[s[<span class="number">0</span>]] = port</span><br><span class="line">		fmt.Printf(<span class="string">"转发表没有该条目，将源地址%s，端口号%s存进转发表\n"</span>, s[<span class="number">0</span>], port)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//	转发帧，查找转发表中目的地址的项目</span></span><br><span class="line">	<span class="keyword">if</span> table[s[<span class="number">1</span>]] == <span class="string">""</span> &#123;</span><br><span class="line">		fmt.Printf(<span class="string">"不存在与目的地址%s对应的条目，通过所有接口转发\n"</span>, s[<span class="number">1</span>])</span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> table[s[<span class="number">1</span>]] == port &#123;</span><br><span class="line">		fmt.Printf(<span class="string">"帧进入和输出的接口都是%s,不转发并丢弃该帧\n"</span>, port)</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		fmt.Printf(<span class="string">"找到与目的地址%s对应的条目，通过接口%s转发\n"</span>, s[<span class="number">1</span>], table[s[<span class="number">1</span>]])</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

                

                <hr>
                <!-- Pager -->
                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/2021/03/01/cob/" data-toggle="tooltip" data-placement="top" title="cob——小巧、便捷的 Go Web 框架">&larr; Previous Post</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/2020/09/16/go-micro使用熔断器中间件/" data-toggle="tooltip" data-placement="top" title="go-micro 使用熔断器中间件">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                <!-- duoshuo Share start -->
                
                <!-- 多说 Share end-->

                <!-- 多说评论框 start -->
                
                <!-- 多说评论框 end -->

                <!-- disqus comment start -->
                
                    <div class="comment">
                        <div id="disqus_thread" class="disqus-thread"></div>
                    </div>
                
                <!-- disqus comment end -->
            </div>
            
            <!-- Tabe of Content -->
            <!-- Table of Contents -->

    
      <aside id="sidebar">
        <div id="toc" class="toc-article">
        <strong class="toc-title">Contents</strong>
        
          <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#null"><span class="toc-nav-number">1.</span> <span class="toc-nav-text">golang 设计令牌锁并发顺序读取文件</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#null"><span class="toc-nav-number">1.1.</span> <span class="toc-nav-text">实现思路</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#null"><span class="toc-nav-number">1.2.</span> <span class="toc-nav-text">完整代码</span></a></li></ol></li></ol>
        
        </div>
      </aside>
    

                
            <!-- Sidebar Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#golang" title="golang">golang</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
                <hr>
                <h5>FRIENDS</h5>
                <ul class="list-inline">

                    
                        <li><a href="https://blog.csdn.net/Congzzzzz" target="_blank">CSDN blog Congz</a></li>
                    
                        <li><a href="https://gitee.com/congz666" target="_blank">Gitee Congz</a></li>
                    
                        <li><a href="http://cmall.congz.top/#/" target="_blank">CMall</a></li>
                    
                </ul>
                
            </div>
        </div>
    </div>
</article>




<!-- disqus embedded js code start (one page only need to embed once) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "your-disqus-ID";
    var disqus_identifier = "http://congz.top/2021/03/01/go令牌锁/";
    var disqus_url = "http://congz.top/2021/03/01/go令牌锁/";

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus embedded js code start end -->




<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'hover',
          placement: 'left',
          icon: 'ℬ'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>



    <!-- Footer -->
    <!-- Footer -->
<footer>
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center">
               
          <li>
            <a
              target="_blank"
              href="https://github.com/congz666"
            >
              <span class="fa-stack fa-lg">
                <i class="fa fa-circle fa-stack-2x"></i>
                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
              </span>
            </a>
          </li>
           
        </ul>
        <p class="copyright text-muted">
          Copyright &copy; Congz 2021
          <br />
          Theme by <a href="http://huangxuan.me">Hux</a>
          <span style="display: inline-block; margin: 0 5px">
            <i class="fa fa-heart"></i>
          </span>
          re-Ported by <a href="http://congz.top">Congz</a>
        </p>
      </div>
    </div>
  </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>

<!-- async load function -->
<script>
  function async(u, c) {
    var d = document,
      t = "script",
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) {
      o.addEventListener(
        "load",
        function (e) {
          c(null, e);
        },
        false
      );
    }
    s.parentNode.insertBefore(o, s);
  }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->

<!-- jquery.tagcloud.js -->
<script>
  // only load tagcloud.js in tag.html
  if ($("#tag_cloud").length !== 0) {
    async(
      "http://congz.top/js/jquery.tagcloud.js",
      function () {
        $.fn.tagcloud.defaults = {
          //size: {start: 1, end: 1, unit: 'em'},
          color: { start: "#bbbbee", end: "#0085a1" },
        };
        $("#tag_cloud a").tagcloud();
      }
    );
  }
</script>

<!--fastClick.js -->
<script>
  async(
    "https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js",
    function () {
      var $nav = document.querySelector("nav");
      if ($nav) FastClick.attach($nav);
    }
  );
</script>

<!-- Google Analytics -->


<script>
  // dynamic User by Hux
  var _gaId = "UA-XXXXXXXX-X";
  var _gaDomain = "congz";

  // Originial
  (function (i, s, o, g, r, a, m) {
    i["GoogleAnalyticsObject"] = r;
    (i[r] =
      i[r] ||
      function () {
        (i[r].q = i[r].q || []).push(arguments);
      }),
      (i[r].l = 1 * new Date());
    (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
    a.async = 1;
    a.src = g;
    m.parentNode.insertBefore(a, m);
  })(
    window,
    document,
    "script",
    "//www.google-analytics.com/analytics.js",
    "ga"
  );

  ga("create", _gaId, _gaDomain);
  ga("send", "pageview");
</script>



<!-- Baidu Tongji -->




	<a id="rocket" href="#top" class=""></a>
	<script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
    <script type="text/javascript" src="/js/toc.js?v=1.0.0" async=""></script>
<!-- Image to hack wechat -->
<img src="http://congz.top/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
